﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using LibrarySync2.Common.Interfaces;

namespace LibrarySync2.Business.FileSystem.Item
{
    [Serializable]
    public abstract class FileSystemItem<TItem> : FileSystemItemBase
        where TItem : FileSystemInfo 
    {
        private readonly TItem _item;

        [XmlIgnore]
        protected TItem Item
        {
            get { return _item; }
        }

        public FileSystemItem()
            : base()
        {
            //Do nothing.  Deserializing.
        }

        internal FileSystemItem(TItem item, string root)
            : base()
        {
            if (item == null)
                throw new ArgumentNullException("item");

            _item = item;
            Name = item.Name;
            FullName = item.FullName;
            Root = root;
            Children.AddRange(LoadChildren());
        }

        protected abstract List<FileSystemInfo> GetChildren();

        private List<IFileSystemItem> LoadChildren()
        {
            var factory = new FileSystemItemFactory();
            var result = new List<IFileSystemItem>();

            foreach (var child in GetChildren())
            {
                result.Add(factory.CreateFileSystemItem(child, Root));
            }

            return result;
        }
    }
}
